package com.calefk.util {
    
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.getTimer;

    public class FadeAlpha {
        
		public static const FADE_IN :int = 1;
		public static const FADE_OUT:int = 2;
        private var mGpx:MovieClip;
        private var mLockClip:Sprite;
        private var mVz:Number;
        
        private var mClassObject:Object  = null;
		private var mCallFunction:Function = null;
		
		private var mOldTime:Number;
        
        public var width :Number;
        public var height:Number;
        
        public function FadeAlpha(clip:MovieClip , width:Number = 600, height:Number = 400) {
            mGpx = clip;
            this.width = width;
            this.height = height;
            drawRect(width , height);
        }
        
		private function drawRect(width:Number , height:Number):void {
			mLockClip = new Sprite();
			mLockClip.x = 0;
			mLockClip.y = 0;
			mGpx.addChild(mLockClip);
            
			with (mLockClip.graphics) {
				beginFill(0x000000, 100);
				lineStyle(1,0x000000, 100);
				lineTo(width, 0);
				lineTo(width, height);
				lineTo(0, height);
				lineTo(0, 0);
				endFill();
			}
			//--Because not Click Again
			mLockClip.addEventListener(MouseEvent.CLICK , function(){});
			mLockClip.useHandCursor = false;
			
			mLockClip.x = 0;
			mLockClip.y = 0;
			mLockClip.alpha = 0;
		}
        
        public function startFade(type:Number, ifunction:Function, time:Number = 1000):void {
			switch(type){
				case FADE_IN:
					showFade(ifunction,time);
					break;
				case FADE_OUT:
					hiddenFade(ifunction,time);
					break;
			}
		}
        
		private function showFade(iFunction:Function, time:Number):void {
			mGpx.alpha = 0;
			mCallFunction = iFunction;
			mGpx.addEventListener(Event.ENTER_FRAME ,onEnterFrameShow, false, 0, true);
			// Get Timer
			mVz = 1/time;
			mOldTime = getTimer();
		}
		
		private function onEnterFrameShow(e:Event):void {
			var t:Number = getTimer() - mOldTime;
			mOldTime = getTimer();
			mGpx.alpha += mVz * t;
			if (mGpx.alpha >= 1) { 
				mGpx.removeEventListener(Event.ENTER_FRAME , onEnterFrameShow); 
				mCallFunction();
			}
		}
		
		private function hiddenFade(iFunction:Function , time:Number):void {
			mGpx.alpha = 1;
			mCallFunction = iFunction;
			mGpx.addEventListener(Event.ENTER_FRAME ,onEnterFrameHidden);
			// Get Timer
			mVz = 1/time;
			mOldTime = getTimer();
		}
		
		private function onEnterFrameHidden(e:Event):void {
			var t:Number = getTimer() - mOldTime;
			mOldTime = getTimer();
			mGpx.alpha -= mVz * t;
			if (mGpx.alpha <= 0) { 
				mGpx.removeEventListener(Event.ENTER_FRAME , onEnterFrameHidden); 
				// call function
				mCallFunction();
			}	
		}
		
		public function clean():void {
            // remove all listeners
            if (mGpx.hasEventListener(Event.ENTER_FRAME)) {
                mGpx.removeEventListener(Event.ENTER_FRAME , onEnterFrameHidden);
                mGpx.removeEventListener(Event.ENTER_FRAME , onEnterFrameShow);
            }
            // remove objects
			mGpx.removeChild(mLockClip);
			mGpx = null;
			mCallFunction = null;
		}        
    }
}